﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MoetutuServer.WebSite.ZeroChan
{
    class TagHelper
    {
        public static void Scanner(String FileName) {
            if (FileName == "")
                return;
            List<string> FileAllLine = Helper.FileHelper.ReadAllLine(FileName);
            List<TagInfo> TagInfoList = new List<TagInfo>();
            foreach (string line in FileAllLine) {
                String[] TagSplit = Regex.Split(line,"-------");
                if (TagSplit.Count() == 2)
                {
                    Console.WriteLine("中文tag"+TagSplit[0]);
                   // Console.WriteLine(TagSplit[1]);
                    int Count = GetImgCount(TagSplit[1]);
                    if (Count > 10) {

                        TagInfo tag = new TagInfo();
                        tag.CnTag1 = TagSplit[0];
                        tag.RomeTag1 = TagSplit[1];
                        tag.Count = Count;
                        TagInfoList.Add(tag);
                      
                    }
                }
            }
            Save(TagInfoList);
        }
        /// <summary>
        /// 保存标签到文件
        /// </summary>
        /// <param name="tagInfoList"></param>
        private static void Save(List<TagInfo> tagInfoList)
        {
            tagInfoList = tagInfoList.OrderBy(t => t.Count).Reverse().ToList();//排序
         
            foreach (TagInfo line in tagInfoList) {
                Console.WriteLine("+++"+line.Count);
                Helper.FileHelper.AppendUTF8Text(".\\分析过的新文件.txt", line.CnTag1 + "-------" + line.RomeTag1);
            }
       
        }

        public static int GetImgCount(string Tag)
        {
            String link = "https://www.zerochan.net/" + System.Web.HttpUtility.UrlEncode(Tag, Encoding.UTF8);
            int count = 0;
            string Shtml = Helper.WebHelper.HttpGet(link);
            if (Verification(Shtml))
                return 100;


            List<string> Tip = Helper.HtmlHelper.GetTextValueList(Shtml, "//p[@id='parents-listing']");
            foreach (string line in Tip) {

                String Text = Regex.Match(line, @"[\w\W]+?anime").Value;
            
                int Multiple = 1;
                if (Text != "") {
                    Console.WriteLine("得到结果："+Text);
                    MatchCollection Count = Regex.Matches(Text, "[0-9]{1}");
                    foreach (Match Matchcount in Count) {
                        Console.WriteLine("取出结果“"+Matchcount.Value);
                        count += Convert.ToInt32(Matchcount.Value) * Multiple;
                        Multiple= Multiple * 10;
                    }
                }
              
            }
            //    Console.WriteLine(Shtml);
        ;
            count = int.Parse(new string(count.ToString().Reverse().ToArray()));
            return count;
        }
        /// <summary>
        /// 数据结构结构体
        /// </summary>
        public struct TagInfo {
            int count;
            string CnTag;
            string RomeTag;
         
            public int Count { get => count; set => count = value; }
            public string CnTag1 { get => CnTag; set => CnTag = value; }
            public string RomeTag1 { get => RomeTag; set => RomeTag = value; }
        }
        /// <summary>
        /// 验证是否是索引页
        /// </summary>
        /// <param name="shtml"></param>
        /// <returns></returns>
        private static bool Verification(string shtml)
        {
            List<string> Tip = Helper.HtmlHelper.GetTextValueList(shtml, "//*[@id=\"children\"]");
            Console.WriteLine(Tip.Count);
            if (Tip.Count > 0)
                return true;
            else
                return false;
        }
    }
}
